package main

import (
	"fmt"
	"medical/sdkInit"
	"medical/service"
	"medical/web"
	"medical/web/controller"
	"os"
)

const (
	cc_name    = "simplecc"
	cc_version = "1.0.0"
)

var SetPath string

// 修改 **54** 行 —— 每个人用对应的docker.yaml
func main() {
	// SetPath = "/workspace/github.com/medical/"
	// SetPath = "/opt/gopath/src/github.com/hyperledger/fabric-samples/medical/"
	SetPath = "/Users/monk/code/go/src/medical/"
	// init orgs information

	orgs := []*sdkInit.OrgInfo{
		{
			OrgAdminUser:  "Admin",
			OrgName:       "Org1",
			OrgMspId:      "Org1MSP",
			OrgUser:       "User1",
			OrgPeerNum:    1,
			OrgAnchorFile: SetPath + "fixtures/channel-artifacts/Org1MSPanchors.tx",
		},
	}

	// init sdk env info
	info := sdkInit.SdkEnvInfo{
		ChannelID:        "mychannel",
		ChannelConfig:    SetPath + "fixtures/channel-artifacts/channel.tx",
		Orgs:             orgs,
		OrdererAdminUser: "Admin",
		OrdererOrgName:   "OrdererOrg",
		OrdererEndpoint:  "orderer.example.com",
		ChaincodeID:      cc_name,
		ChaincodePath:    SetPath + "chaincode/",
		ChaincodeVersion: cc_version,
	}

	// sdk setup
	sdk, err := sdkInit.Setup("config_monk.yaml", &info)
	// sdk, err := sdkInit.Setup("config.yaml", &info)
	// sdk, err := sdkInit.Setup("config_jack.yaml", &info)
	if err != nil {
		fmt.Println(">> SDK setup error:", err)
		os.Exit(-1)
	}

	// create channel and join
	if err := sdkInit.CreateAndJoinChannel(&info); err != nil {
		fmt.Println(">> Create channel and join error:", err)
		os.Exit(-1)
	}

	// create chaincode lifecycle
	if err := sdkInit.CreateCCLifecycle(&info, 1, false, sdk); err != nil {
		fmt.Println(">> create chaincode lifecycle error: %v", err)
		os.Exit(-1)
	}

	// invoke chaincode set status
	fmt.Println(">> 通过链码外部服务设置链码状态......")
	serviceSetup, err := service.InitService(info.ChaincodeID, info.ChannelID, info.Orgs[0], sdk)
	if err != nil {
		fmt.Println()
		os.Exit(-1)
	}

	//在main里先把baseinfo的数据上链
	//调用 函数 service/InitUpload
	//input: null, output: args []string

	//将operation_record的操作记录数据上链
	//调用 函数 service/InitOperationRecord

	BlockInfo, err := serviceSetup.InitUpload("Admin", "卫生监督管理局")
	if err != nil {
		fmt.Println(err.Error())
	} else {
		fmt.Println(BlockInfo.FirstBlock, BlockInfo.SecondBlock, BlockInfo.ThirdBlock)
	}

	app := controller.Application{
		Setup: serviceSetup,
	}
	web.WebStart(app)
}

// func main() {
// 	req := abac.ABACRequest{
// 		Sub: "u1",
// 		Obj: "d1",
// 		Op:  "r",
// 	}
// 	req.CurTime = time.Now().Unix()
// 	Parg := "{\"obj\":\"08373bd6453bd\",\"owner\":\"赵七\",\"Env\":{\"allowOrg\":\"北大六院;北京天坛医院;天津医科大学附属医院;山东大学齐鲁医院\",\"createdTime\":\"2023-03-12 15:25:12\",\"endTime\":\"2024-03-12 15:03:25\"},\"subRules\":[\"role:u2,action:r,researce:神经系统疾病\",\"role:u3,action:-\",\"role:admin,action:all\",\"role:u1,action:rw,researce:神经系统疾病\"]}"
// 	user := abac.Sub{
// 		Role:  "u1",
// 		Org:   "北大六院",
// 		Group: "神经系统疾病",
// 	}
// 	abac.CheckAccess(req, Parg, user)
// }
